package com.yifeng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yifeng.dao.BaseMapper;
import com.yifeng.service.IBaseService;
import com.yifeng.util.LayuiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

@Service
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    public void add(T t) {
        baseMapper.save(t);
    }

    public void delete(Serializable id) {
        baseMapper.remove(id);
    }

    public void update(T t) {
        baseMapper.update(t);
    }

    public T getById(Serializable id) {
        return baseMapper.loadById(id);
    }

    public List<T> getAll() {
        return baseMapper.loadAll();
    }

    public LayuiResult<T> getPageAll(int page,int rows) {

        LayuiResult<T> result = new LayuiResult<T>();
        PageHelper.startPage(page,rows);//开始分页,物理分页

        List<T> list = baseMapper.loadAll();
        PageInfo<T> pageInfo = new PageInfo<T>(list);//pagehelper分页数据
        result.setData(pageInfo.getList());
        result.setCount((int) pageInfo.getTotal());
        result.setLimit(rows);
        return result;
    }

}
